package com.ng.desktop.expansionsa.view.cliente;

import java.awt.Window;

import javax.swing.JOptionPane;

import org.apache.commons.lang.StringUtils;

import com.ng.desktop.expansionsa.model.Cliente;
import com.ng.desktop.expansionsa.model.TipoCliente;
import com.ng.desktop.expansionsa.persistence.ClientePersistence;
import com.ng.framework.view.desktop.NGEntitySelectorField;
import com.ng.framework.view.desktop.NGGUIUtils;
import com.ng.framework.view.desktop.NGTablePanel;

public class ClienteTablePanel extends NGTablePanel<Cliente, ClientePersistence> {

	private static final long serialVersionUID = 3622830473453308772L;

	private static final String[] COLUMN_NAMES = new String[] { "Id",
			"Razon Social / Nombre", "CUIT / DNI", "Dirección", "Tel. Fijo",
			"Celular" };

	private static final int[] COLUMN_WIDTHS = { 50, 240, 100, 350, 100, 100 };

	public ClienteTablePanel(ClientePersistence persistence) {
		super(persistence);
	}

	public ClienteTablePanel(
			ClientePersistence persistence,
			NGEntitySelectorField<Cliente, ClientePersistence> ngEntitySelectorField) {
		super(persistence, ngEntitySelectorField);
	}

	@Override
	protected Object[] toRow(Cliente dataObject) {
		Object[] row = null;

		String telefonoFijo = "";
		if (StringUtils.isNotBlank(dataObject.getTelefonoFijo())) {
			telefonoFijo = "(" + dataObject.getPrefijoTelefonoFijo() + ") "
					+ dataObject.getTelefonoFijo();
		}
		String telefonoCelular = "";
		if (StringUtils.isNotBlank(dataObject.getTelefonoCelular())) {
			telefonoCelular = "(" + dataObject.getPrefijoTelefonoCelular()
					+ ") 15" + dataObject.getTelefonoCelular();
		}
		if (dataObject.getTipoCliente().equals(TipoCliente.EMPRESA)) {
			row = new Object[] { dataObject.getId(),
					dataObject.getRazonSocial(), dataObject.getCuit(),
					dataObject.getDireccion(), telefonoFijo, telefonoCelular };
		} else {
			row = new Object[] { dataObject.getId(),
					dataObject.getApellido() + ", " + dataObject.getNombre(),
					dataObject.getDni(), dataObject.getDireccion(),
					telefonoFijo, telefonoCelular };
		}

		return row;
	}

	@Override
	protected String[] getColumnNames() {
		return COLUMN_NAMES;
	}

	@Override
	protected int[] getColumnWidths() {
		return COLUMN_WIDTHS;
	}

	@Override
	protected boolean edit(ClientePersistence persistence, Cliente instance) {
		Window owner = NGGUIUtils.findWindow(this);
		
		ClienteEditDialog dialog = new ClienteEditDialog(JOptionPane.getFrameForComponent(this), persistence, instance,
				entitySelection, true);
		dialog.setLocationRelativeTo(owner);
		dialog.setVisible(true);
		
		return true;
	}

	@Override
	protected Cliente getSelectedInstance(ClientePersistence persistence,
			Object[] rowData) {
		return persistence.findById((Long) rowData[0]);
	}

	@Override
	protected boolean create(ClientePersistence persistence) {
		Window owner = NGGUIUtils.findWindow(this);
		
		ClienteEditDialog dialog = new ClienteEditDialog(owner, persistence, false);
		dialog.setLocationRelativeTo(owner);
		dialog.setVisible(true);
		
		return true;
	}

}
